import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test1/bean/BaseBean.dart';
import 'http.dart';

class SRHttpRequest {
  late YYRequestManager requestManager;
  late String url;
  late String htmlUrl;

  void init(
      {String url = '', String appVersion = '1.4.5', String htmlUrl = ''}) {
    this.url = url;
    this.htmlUrl = htmlUrl;
    requestManager = new YYRequestManager(
      baseUrl: url,
      baseHeaders: {
        "version": appVersion,
        "source": Platform.isAndroid? "4":"5",
      },
    );
  }

  SRHttpRequest._();

  static final SRHttpRequest _instance = SRHttpRequest._();

  static SRHttpRequest getInstance() {
    return _instance;
  }

  Future<YYResultModel<BaseBean>> login({
    BuildContext? context,
    CancelToken? cancelToken,
    required String username,
    required String password,
    required String token,//极光注册ID
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["username"] = username;
    p["password"] = password;
    p["token"] = token;
    YYResultModel<BaseBean> result = await requestManager.requestPost(
      _makeUrl("app/shop/login"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  Future<YYResultModel<BaseBean>> newsList({
    BuildContext? context,
    CancelToken? cancelToken,
    required int defaultCurrent,
    required int pageSize,
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["defaultCurrent"] = defaultCurrent;
    p["pageSize"] = pageSize;
    YYResultModel<BaseBean> result = await requestManager.requestGet(
      _makeUrl("app/shop/querySchoolInfo"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  Future<YYResultModel<BaseBean>> categoryList({
    BuildContext? context,
    CancelToken? cancelToken,
    required String shopId,
    required int defaultCurrent,
    required int pageSize,
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["shopId"] = shopId;
    p["defaultCurrent"] = defaultCurrent;
    p["pageSize"] = pageSize;
    YYResultModel<BaseBean> result = await requestManager.requestGet(
      _makeUrl("app/shop/v1/queryShopGoodsType"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  Future<YYResultModel<BaseBean>> addCategory({
    BuildContext? context,
    CancelToken? cancelToken,
    required String shopId,
    required String typeName,
    required String goodsTypeImage,
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["shopId"] = shopId;
    p["typeName"] = typeName;
    p["goodsTypeImage"] = goodsTypeImage;
    YYResultModel<BaseBean> result = await requestManager.requestPost(
      _makeUrl("app/shop/v1/saveShopGoodsType"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  Future<YYResultModel<BaseBean>> updateCategory({
    BuildContext? context,
    CancelToken? cancelToken,
    required String shopId,
    required String shopGoodsTypeId,
    required String typeName,
    required String goodsTypeImage,
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["shopId"] = shopId;
    p["typeName"] = typeName;
    p["goodsTypeImage"] = goodsTypeImage;
    p["shopGoodsTypeId"] = shopGoodsTypeId;
    YYResultModel<BaseBean> result = await requestManager.requestPost(
      _makeUrl("app/shop/v1/saveShopGoodsType"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  Future<YYResultModel<BaseBean>> deleteCategory({
    BuildContext? context,
    CancelToken? cancelToken,
    required String shopGoodsTypeId,
  }) async {
    Map<String, dynamic> p = new Map<String, dynamic>();
    p["shopGoodsTypeId"] = shopGoodsTypeId;
    YYResultModel<BaseBean> result = await requestManager.requestPost(
      _makeUrl("app/shop/v1/deleteShopGoodsType"),
      p,
          (val) {
        return BaseBean.fromJson(val);
      },
      context: context,
      cancelToken: cancelToken,
    );
    return result;
  }

  String _makeUrl(
    String u, {
    String version = "v1",
  }) {
    return u;
  }

  String getHtmlUrl(String path) {
    String result = htmlUrl + path;
    return result;
  }
}
